package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.User;
import com.powernode.dto.UserDto;
import com.powernode.service.UserService;
import com.powernode.utils.MD5Utils;
import com.powernode.utils.WebMvcUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
@Api(tags = "用户的管理接口")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param userDto
     * @return
     */
    @GetMapping("list.do")
    @ApiOperation("分页查询")
    public Result list(UserDto userDto) {
        PageInfo<User> pageInfo = userService.queryAllUser(userDto);
        return new Result(0, "查询成功", pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加
     *
     * @param user
     * @return
     */
    @PostMapping("addUser.do")
    @ApiOperation("添加")
    public Result addUser(User user) {
        try {
            user.setPassword(Constants.USER_DEFAULT_PWD);
            user.setDeleted(Constants.DELETED_1);
            user.setCreateTime(new Date());
            userService.insertSelective(user);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @PostMapping("updateUser.do")
    @ApiOperation("修改")
    public Result updateUser(User user) {
        try {
            //数据库中时间戳类型会自动修改
            //user.setUpdateTime(new Date());
            userService.updateByPrimaryKeySelective(user);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 修改离职
     *
     * @param
     * @return
     */
    @PostMapping("quit.do")
    @ApiOperation("修改离职")
    public Result quit(Integer id) {
        try {
            User user = new User();
            user.setId(id);
            user.setDeleted(Constants.DELETED_2);
            userService.updateByPrimaryKeySelective(user);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 重置密码
     *
     * @param
     * @return
     */
    @PostMapping("resetPwd.do")
    @ApiOperation("重置密码")
    public Result resetPwd(Integer id) {
        try {
            User user = new User();
            user.setId(id);
            user.setPassword(MD5Utils.md5(Constants.USER_DEFAULT_PWD));
            userService.updateByPrimaryKeySelective(user);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequestMapping("queryAllAvailableUser.do")
    @ApiOperation("条件查询")
    public Result queryAllAvailableUser() {
        List<User> userList = userService.queryAllAvailableUser();
        return new Result(200, "查询成功", 0L, userList);
    }

    @RequestMapping("changePwd.do")
    @ApiOperation("修改密码")
    public Result changePwd(String oldPassword, String password, String password2) {
        try {
            User user = WebMvcUtils.getCurrentUser();
            if (oldPassword != null && password != null && password2 != null) {
                if (user.getPassword().equals(MD5Utils.md5(oldPassword))) {
                    if (password.equals(password2)) {
                        user.setPassword(MD5Utils.md5(password));
                        userService.updateByPrimaryKeySelective(user);
                        return Result.OPT_SUCCESS;
                    }
                }
            }
            return Result.OPT_ERROR;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }
}
